package com.qiche.util.wx;


import com.alibaba.fastjson.JSONObject;
import com.qiche.menu.Button;
import com.qiche.menu.ClickButton;
import com.qiche.menu.Menu;
import com.qiche.menu.ViewButton;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * ΢微信工具类
 *
 * @author huqingfeng
 */
@Component
public class WeixinUtil {

//	private static final String APPID = "wxf3e5c640be507755";
//
//	private static final String APPSECRET = "69758ca5a5a937ccfb981fc56a00fa2a";

    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    private static final String CODE_URI = "http://open.weixin.qq.com/connect/oauth2/authorize";

    //获取access_token接口
    private static final String ACCESS_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";

    //根据openid和access_token查询用户信息接口
    private static final String BASE_INFORMATION = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    private static final String UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";

    //自定义菜单创建接口
    private static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
    //自定义菜单查询接口
    private static final String QUERY_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
    //自定义菜单删除接口
    private static final String DELETE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";


    /**
     * get请求
     * @param url
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public JSONObject doGetStr(String url) throws ParseException, IOException {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        JSONObject jsonObject = null;
        HttpResponse httpResponse = client.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            jsonObject = JSONObject.parseObject(result);
        }
        return jsonObject;
    }

    /**
     * POST请求
     *
     * @param url
     * @param outStr
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static  JSONObject doPostStr(String url, String outStr) throws ParseException, IOException {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost httpost = new HttpPost(url);
        JSONObject jsonObject = null;
        httpost.setEntity(new StringEntity(outStr, "UTF-8"));
        HttpResponse response = client.execute(httpost);
        String result = EntityUtils.toString(response.getEntity(), "UTF-8");
        jsonObject = JSONObject.parseObject(result);
        return jsonObject;
    }

    /**
     * 获取tokenUrl
     *
     * @param code
     * @return
     */
    public String getAccessTokenUrl(String code,String wxAppid,String wxSecret) {
        System.out.println("APPID---------" + wxAppid);
        System.out.println("APPSECRET--------------" + wxSecret);
        String url = ACCESS_TOKEN.replace("APPID", wxAppid)
                .replace("SECRET", wxSecret)
                .replace("CODE", code);
        return url;
    }

    /**
     * @param accessToken
     * @param openid
     * @return
     */
    public String getBaseInformationUrl(String accessToken, String openid) {
        String url = BASE_INFORMATION.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openid);
        return url;
    }

    public static Menu initMenu(){
        Menu menu=new Menu();
        ClickButton clickButton=new ClickButton();
        clickButton.setName("click菜单");
        clickButton.setType("click");
        clickButton.setKey("111");

        ViewButton viewButton=new ViewButton();
        viewButton.setName("view菜单");
        viewButton.setType("view");
        viewButton.setUrl("https://qiche.yhulian.cn");

        ClickButton clickButton2=new ClickButton();
        clickButton2.setName("扫码事件");
        clickButton2.setType("scancode_push");
        clickButton2.setKey("31");

        ClickButton clickButton3=new ClickButton();
        clickButton3.setName("地理位置");
        clickButton3.setType("location_select");
        clickButton3.setKey("32");

        Button button=new Button();
        button.setName("菜单");
        button.setSub_button(new Button[]{clickButton2,clickButton3});

        menu.setButton(new Button[]{clickButton,viewButton,button});
        return  menu;

    }


    public static int createMenu(String token,String menu) throws IOException {
        int result=0;
        String url=CREATE_MENU_URL.replace("ACCESS_TOKEN",token);
        JSONObject jsonObject=doPostStr(url,menu);
        if(jsonObject!=null){
            result=jsonObject.getInteger("errcode");
        }
        return result;
    }

    public static void main(String[] args) {
       /* String url = "http://demo2.1hulian.cn/wap/check?merchantId=1";
        try {
            String encode = java.net.URLEncoder.encode(url, "utf-8");
            System.out.println(encode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/

    }
}
